﻿
Namespace PL.MESSDK
    Public Class SpecificationTestRevision_PL
        Inherits PLBase
        Implements IPLProcess


        Public Property SpecificationRevId_FK As Guid?

        Private _aQueryName As String


        Public Sub New()
            MyBase.New()
        End Sub
        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
            _aQueryName = queryName
        End Sub




        Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating
            'Code goes There 
        End Sub

        Public Overrides Function Delete() As Boolean
            'Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function

        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function

        Public Overrides Sub ValidateRow(row As DataRow)
             row.ClearErrors()

            If row.Table.Columns.Contains("TestId_FK") Then
                If IsDBNull(row("TestId_FK")) Then
                    row.SetColumnError("TestId_FK", "Mandatory")
                End If
            End If

            If row.Table.Columns.Contains("CompulsoryRep") Then
                If IsDBNull(row("CompulsoryRep")) Then
                    row.SetColumnError("CompulsoryRep", "Mandatory")
                End If
            End If
            If row.Table.Columns.Contains("Compulsory") Then
                If IsDBNull(row("Compulsory")) Then
                    row.SetColumnError("Compulsory", "Mandatory")
                End If
            End If

            If row.HasErrors Then
                row.RowError = "Please check your columns, a validation error occur"
            End If

        End Sub

        Private Function RowIsUniqueIndex(ByVal row As DataRow)
            'When adding a new SpecificationTest, the combination of columns 
            'OperationId_FK, TestSpecificationCode, SOP & Method, must be UNIQUE within a same Specification Revision. 
            'dataTable.Select("FieldName is NULL")
            'http://forums.asp.net/t/705965.aspx/1

            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing
            Try
                Dim strFilter As String = String.Format("SpecificationRevId_FK = '{0}' and OperationId_FK = '{1}' and TestSpecificationCode = '{2}' and SOP = '{3}' and Method = '{4}'", row("SpecificationRevId_FK"), row("OperationId_FK"), row("TestSpecificationCode"), row("SOP"), row("Method"))

                If Me.MetaDataTable.Select(strFilter).Count > 0 Then
                    Return False
                Else
                    Return True
                End If

            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try
        End Function


        Public Function BatchRecords(dataRow() As System.Data.DataRow) As Boolean Implements IPLProcess.BatchRecords
            Return True
        End Function

        Public Function CurrentRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.CurrentRecord
            Return True
        End Function

        Public Function NewRecord(dataRow As System.Data.DataRow) As Boolean Implements IPLProcess.NewRecord
            'Si on arrive d'un TreeList...

            If SpecificationRevId_FK IsNot Nothing Then
                dataRow("SpecificationRevId_FK") = SpecificationRevId_FK
            End If

            If IsDBNull(dataRow("Compulsory")) Then
                dataRow("Compulsory") = 0
            End If

            If IsDBNull(dataRow("CompulsoryRep")) Then
                dataRow("CompulsoryRep") = 0
            End If

            dataRow("SpecificationTestRevId") = Guid.NewGuid
            dataRow("SpecificationTestId_FK") = Guid.NewGuid

            Return True

        End Function

        Public Shared Function ReviseSpecTest(ByVal specTestRevID As Guid, ByVal specTestID As Guid)
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("SpecificationTestRevision_Revise")

                sqlcmd.Parameters("@SpecificationTestRevId").Value = specTestRevID
                sqlcmd.Parameters("@SpecificationTestId").Value = specTestID

                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)

            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function

        Public Shared Function LimitSpecTest(ByVal specTestRevID As Guid, ByVal specTestID As Guid)
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("SpecificationTestRevision_Limite")

                sqlcmd.Parameters("@SpecificationTestRevId").Value = specTestRevID
                sqlcmd.Parameters("@SpecificationTestId").Value = specTestID

                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)

            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Dispose()
                End If
            End Try

            Return True
        End Function
    End Class


End Namespace
